﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics;

namespace opentk_first_project
{
    public class Sprite
    {
        internal const int VertexAmount = 6;
        Vector3d[] _vertexPositions = new Vector3d[VertexAmount];
        Color4[] _vertexColors = new Color4[VertexAmount];
        Vector2[] _vertexUVs = new Vector2[VertexAmount];
        Texture _texture = new Texture();
        public Sprite()
        {
            InitVertexPositions(new Vector3d(0, 0, 0), 1, 1);
            SetColor4(new Color4(1.0f, 1.0f, 1.0f, 1.0f));
            SetUVs(new Vector2(0, 0), new Vector2(1, 1));
        }
        public Texture Texture
        {
            get { return _texture; }
            set
            {
                _texture = value;
                // By default the width and height is set
                // to that of the texture
                InitVertexPositions(GetCenter(), _texture.Width, _texture.
                Height);
            }
        }
        public Vector3d[] VertexPositions
        {
            get { return _vertexPositions; }
        }
        public Color4[] VertexColors
        {
            get { return _vertexColors; }
        }
        public Vector2[] VertexUVs
        {
            get { return _vertexUVs; }
        }
        private Vector3d GetCenter()
        {
            double halfWidth = GetWidth() / 2;
            double halfHeight = GetHeight() / 2;
            return new Vector3d(
            _vertexPositions[0].X + halfWidth,
            _vertexPositions[0].Y - halfHeight,
            _vertexPositions[0].Z);
        }
        private void InitVertexPositions(Vector3d position, double width, double height)
        {
            double halfWidth = width / 2;
            double halfHeight = height / 2;
            // Clockwise creation of two triangles to make a quad.
            // TopLeft, TopRight, BottomLeft
            _vertexPositions[0] = new Vector3d(position.X - halfWidth, position.Y +
                halfHeight, position.Z);
            _vertexPositions[1] = new Vector3d(position.X + halfWidth, position.Y +
                halfHeight, position.Z);
            _vertexPositions[2] = new Vector3d(position.X - halfWidth, position.Y -
                halfHeight, position.Z);
            // TopRight, BottomRight, BottomLeft
            _vertexPositions[3] = new Vector3d(position.X + halfWidth, position.Y +
                halfHeight, position.Z);
            _vertexPositions[4] = new Vector3d(position.X + halfWidth, position.Y -
                halfHeight, position.Z);
            _vertexPositions[5] = new Vector3d(position.X - halfWidth, position.Y -
                halfHeight, position.Z);
        }
        public double GetWidth()
        {
            // top right - top left
            return _vertexPositions[1].X - _vertexPositions[0].X;
        }
        public double GetHeight()
        {
            // top left - bottom left
            return _vertexPositions[0].Y - _vertexPositions[2].Y;
        }
        public void SetWidth(float width)
        {
            InitVertexPositions(GetCenter(), width, GetHeight());
        }
        public void SetHeight(float height)
        {
            InitVertexPositions(GetCenter(), GetWidth(), height);
        }
        public void SetPosition(double x, double y)
        {
            SetPosition(new Vector3d(x, y, 0));
        }
        public void SetPosition(Vector3d position)
        {
            InitVertexPositions(position, GetWidth(), GetHeight());
        }
        public void SetColor4(Color4 Color4)
        {
            for (int i = 0; i < Sprite.VertexAmount; i++)
            {
                _vertexColors[i].A = Color4.A;
                _vertexColors[i].R = Color4.R;
                _vertexColors[i].G = Color4.G;
                _vertexColors[i].B = Color4.B;                
            }
        }
        public void SetUVs(Vector2 topLeft, Vector2 bottomRight)
        {
            // TopLeft, TopRight, BottomLeft
            _vertexUVs[0] = topLeft;
            _vertexUVs[1] = new Vector2(bottomRight.X, topLeft.Y);
            _vertexUVs[2] = new Vector2(topLeft.X, bottomRight.Y);
            // TopRight, BottomRight, BottomLeft
            _vertexUVs[3] = new Vector2(bottomRight.X, topLeft.Y);
            _vertexUVs[4] = bottomRight;
            _vertexUVs[5] = new Vector2(topLeft.X, bottomRight.Y);
        }
    }
}
